#include <QtWidgets>
#include "settingdialog.h"
#include "settings.h"
#include "adbutils.h"

SettingDialog::SettingDialog(QWidget *parent)
 : QDialog(parent)
 , ui(new Ui::Setting)
{
    ui->setupUi(this);
    Settings *settings = Settings::getInstance();
    ui->label_adbPath->setText(settings->getAdbPath());

    int width = settings->getDisWidth();
    int dWidth, dHeight;
    int height = AdbUtils::getDisHeight(width, dWidth, dHeight);
    ui->label_width->setText(QString::number(dWidth));
    ui->label_height->setText(QString::number(dHeight));
    ui->spinBox_disWidth->setValue(width);
    ui->spinBox_disHeight->setValue(height);
}

void SettingDialog::onClickAdbPathButton() {
    QString fileName = QFileDialog::getOpenFileName(this,
            tr("Select adb tool"), "",
            tr("adb (adb*)"));
    if (fileName.isEmpty())
           return;
    Settings *settings = Settings::getInstance();
    settings->setAdbPath(fileName);
    ui->label_adbPath->setText(fileName);
    qDebug() << "set adb path" << fileName;
}

void SettingDialog::accept() {
    int width = ui->spinBox_disWidth->value();
    if (width >= 100){
        Settings *settings = Settings::getInstance();
        settings->setDisWidth(width);
        QDialog::accept();
    } else {
        if (QMessageBox::Yes == QMessageBox::critical(this, QString("Error"),QString("显示宽度必须大于等于100!"), QMessageBox::Yes))
            QDialog::reject();
    }
}
